package com.atguigu.boot04.controller;

import com.atguigu.boot04.bean.User;
import com.atguigu.boot04.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;

/**
 * @author:朱思博
 * @date:2021/5/5-{20:52}   table目录下的页面跳转
 */
@Controller
public class TableController {

    // 前往basic_table界面
    @GetMapping("/basic_table")
    public String basic_table() {
        return "table/basic_table";
    }


    @Autowired
    UserService userService;

    // 前往dynamic_table页面
    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model) {
//        // 动态遍历表格内容
//        List<User> users = Arrays.asList(
//                new User("zhangsan", "123"),
//                new User("lisi", "456"),
//                new User("sisiu", "1")
//        );
//
//        model.addAttribute("users",users);


        // 整合Mybatis-Plus 从数据库中查出user表中数据
//        List<User> list = userService.list();
//        model.addAttribute("users",list);

        // 分页查询数据
        Page<User> userPage = new Page<>(pn, 2);
        // 分页查询的结果
        Page<User> page = userService.page(userPage, null);
        model.addAttribute("page",page);
        return "table/dynamic_table";
    }

    @Autowired
    RedisTemplate redisTemplate;
    // 前往responsive_table页面
    @GetMapping("/responsive_table")
    public String responsive_table() {
        // 连接本机redis的简单测试
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("name","sisiu");
        System.out.println(valueOperations.get("name"));

        return "table/responsive_table";
    }

    // 前往editable_table页面
    @GetMapping("/editable_table")
    public String editable_table() {
        return "table/editable_table";
    }
}
